package com.plian.system.service.im.impl;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.BigDecimalUtil;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.im.ExecutionProjectSituation;
import com.plian.system.mapper.im.ExecutionProjectSituationMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.im.IExecutionProjectSituationService;
import com.plian.system.vo.im.ExecutionProjectSituationVO;
import com.plian.system.wrapper.im.ExecutionProjectSituationWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author
 * @since 2019-09-27
 */
@Slf4j
@Service
@AllArgsConstructor
public class ExecutionProjectSituationServiceImpl extends BaseServiceImpl<ExecutionProjectSituationMapper, ExecutionProjectSituation> implements IExecutionProjectSituationService {

	private ExecutionProjectSituationWrapper executionProjectSituationWrapper;

	@Override
	public boolean saveOrUpdate(ExecutionProjectSituation executionProjectSituation) {
		return super.saveOrUpdate(executionProjectSituation);
	}

	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public boolean deleteLogic(List<Long> ids) {
		for (Long id : ids) {
			ExecutionProjectSituation executionProjectSituation = getById(id);
			if (executionProjectSituation == null) {
				return false;
			}
		}
		return super.deleteLogic(ids);
	}

	@Override
	public boolean deleteByParentId(List<Long> ids) {
		LambdaQueryWrapper<ExecutionProjectSituation> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.in(ExecutionProjectSituation::getEffectiveInvestmentId, ids);
		List<ExecutionProjectSituation> mainProjectSituations = list(queryWrapper);
		if (CollectionUtil.isNotEmpty(mainProjectSituations)){
			return deleteLogic(mainProjectSituations.stream().map(ExecutionProjectSituation::getId).collect(Collectors.toList()));
		}
		return true;
	}

	@Override
	public void write(Long effectiveId, ExcelWriter excelWriter, WriteSheet writeSheet, FillConfig fillConfig) {
		List<ExecutionProjectSituationVO> stakeVOS =executionProjectSituationWrapper
				.entityToVO(list(new LambdaQueryWrapper<ExecutionProjectSituation>().eq(ExecutionProjectSituation::getEffectiveInvestmentId, effectiveId)
				.eq(ExecutionProjectSituation::getProjectType, 1)));
		if (CollectionUtil.isNotEmpty(stakeVOS)){
			stakeVOS.add(getsubTotal(stakeVOS));
		}
		List<ExecutionProjectSituationVO> fixedVOS = executionProjectSituationWrapper
				.entityToVO(list(new LambdaQueryWrapper<ExecutionProjectSituation>().eq(ExecutionProjectSituation::getEffectiveInvestmentId, effectiveId)
						.eq(ExecutionProjectSituation::getProjectType, 2)));
		if (CollectionUtil.isNotEmpty(fixedVOS)){
			fixedVOS.add(getsubTotal(fixedVOS));
		}
		excelWriter.fill(new FillWrapper("stake", stakeVOS), fillConfig, writeSheet);
		excelWriter.fill(new FillWrapper("fixed", fixedVOS), fillConfig, writeSheet);
	}

	private ExecutionProjectSituationVO getsubTotal(List<ExecutionProjectSituationVO> situationVOS){
		ExecutionProjectSituationVO subTotal = new ExecutionProjectSituationVO();
		subTotal.setProjectName("合计");
		for (ExecutionProjectSituationVO situationVO : situationVOS){
			subTotal.setPlanTotalInvestment(BigDecimalUtil.add(subTotal.getPlanTotalInvestment(), situationVO.getPlanTotalInvestment()));
			subTotal.setLastCompleteInvestment(BigDecimalUtil.add(subTotal.getLastCompleteInvestment(), situationVO.getLastCompleteInvestment()));
			subTotal.setCompleteInvestment(BigDecimalUtil.add(subTotal.getCompleteInvestment(), situationVO.getCompleteInvestment()));
		}
		return subTotal;
	}
}
